import request from './request';

// 获取客户列表
export function getCustomerList(params) {
  console.log('调用客户列表API, 参数:', params);
  return request({
    url: '/api/omm/customers/',
    method: 'get',
    params
  }).then(response => {
    console.log('客户列表API返回:', response);
    return response;
  }).catch(error => {
    console.error('客户列表API错误:', error);
    throw error;
  });
}

// 获取单个客户
export function getCustomer(id) {
  console.log('获取客户详情, ID:', id);
  return request({
    url: `/api/omm/customers/${id}/`,
    method: 'get'
  }).then(response => {
    console.log('客户详情API返回:', response);
    return response;
  }).catch(error => {
    console.error('获取客户详情错误:', error);
    throw error;
  });
}

// 新增客户
export function createCustomer(data) {
  console.log('创建客户, 数据:', data);
  return request({
    url: '/api/omm/customers/',
    method: 'post',
    data
  }).then(response => {
    console.log('创建客户API返回:', response);
    return response;
  }).catch(error => {
    console.error('创建客户错误:', error);
    throw error;
  });
}

// 更新客户
export function updateCustomer(id, data) {
  console.log('更新客户, ID:', id, '数据:', data);
  return request({
    url: `/api/omm/customers/${id}/`,
    method: 'put',
    data
  }).then(response => {
    console.log('更新客户API返回:', response);
    return response;
  }).catch(error => {
    console.error('更新客户错误:', error);
    throw error;
  });
}

// 删除客户
export function deleteCustomer(id) {
  console.log('删除客户, ID:', id);
  return request({
    url: `/api/omm/customers/${id}/`,
    method: 'delete'
  }).then(response => {
    console.log('删除客户API返回:', response);
    return response;
  }).catch(error => {
    console.error('删除客户错误:', error);
    throw error;
  });
}

// 导出客户数据
export function exportCustomers(params) {
  console.log('导出客户数据, 参数:', params);
  return request({
    url: '/api/omm/customers/export/',
    method: 'get',
    params,
    responseType: 'blob'
  }).then(response => {
    console.log('导出客户数据成功');
    return response;
  }).catch(error => {
    console.error('导出客户数据错误:', error);
    throw error;
  });
}

// 导入客户数据
export function importCustomers(data) {
  console.log('导入客户数据');
  return request({
    url: '/api/omm/customers/import_data/',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => {
    console.log('导入客户数据API返回:', response);
    return response;
  }).catch(error => {
    console.error('导入客户数据错误:', error);
    throw error;
  });
}

// 下载导入模板
export function getImportTemplate() {
  console.log('下载客户导入模板');
  return request({
    url: '/api/omm/customers/import_template/',
    method: 'get',
    responseType: 'blob'
  }).then(response => {
    console.log('下载客户导入模板成功');
    return response;
  }).catch(error => {
    console.error('下载客户导入模板错误:', error);
    throw error;
  });
} 